package com.hcx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcx.reggie.dto.OrdersDto;
import com.hcx.reggie.pojo.AddressBook;
import com.hcx.reggie.pojo.Orders;
import com.hcx.reggie.pojo.User;
import com.hcx.reggie.service.AddressBookService;
import com.hcx.reggie.service.OrderService;
import com.hcx.reggie.service.UserService;
import com.hcx.reggie.utils.BaseContext;
import com.hcx.reggie.utils.R;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("page")
    public R<Page<Orders>> page(int page, int pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        log.info("{},{}", beginTime, endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.like(number != null, Orders::getNumber, number);
        orderWrapper.ge(beginTime != null, Orders::getOrderTime, beginTime);
        orderWrapper.le(endTime != null, Orders::getCheckoutTime, endTime);
        orderService.page(ordersPage, orderWrapper);
        List<Orders> records = ordersPage.getRecords();
        records = records.stream().map((item) -> {
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            String name = user.getName();
            if (name == null) {
                item.setUserName("匿名用户");
            } else {
                item.setUserName(name);
            }
            return item;
        }).collect(Collectors.toList());
        ordersPage.setRecords(records);
        return R.success(ordersPage);
    }


    /**
     * 接受订单
     *
     * @param orders 订单id
     * @return 成功或者失败的提示信息
     */
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders) {
        R<String> r = orderService.submit(orders);
        return r;


    }
    @PutMapping()
    public R<String> update(@RequestBody Orders orders) {
        Long ordersId = orders.getId();
        Orders orderServiceById = orderService.getById(ordersId);
        orderServiceById.setCheckoutTime(LocalDateTime.now());
        orderServiceById.setStatus(3);
        orderService.updateById(orderServiceById);
        return R.success("订单已完成");
    }
    @GetMapping("userPage")
    public R<Page<Orders>> userPage(int page, int pageSize) {
        //获取用户的id
        Long id = BaseContext.getCurrentId();
        //封装page对象
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        //查询订单
        LambdaQueryWrapper<Orders> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(Orders::getUserId, id);
        orderService.page(ordersPage, orderWrapper);
        return R.success(ordersPage);


    }

}
